<?php
namespace app\model\user;

use think\facade\Cache;
use think\Model;

class DouyinAreaModel extends Model
{
    protected $name = 'douyin_area';

    public static function getAreaList()
    {
        $area_list = Cache::get('area_list');
        if (!$area_list) {
            $area_list = DouyinAreaModel::whereIn('level',[1,2,3])->select()->toArray();
            $area_list = self::build_department_child('', $area_list);
            Cache::set('area_list', $area_list);
        }
        return $area_list;
    }

    /**
     * 递归获取
     * 
     * @return array
     */
    public static function build_department_child($parentid, $area_list, $ids = []){
        $treeList = [];
        foreach ($area_list as  $key => $v) {
            if ($parentid == $v['parent_id']) {
                $v['spread'] = false;
                $node = $v;
                $child = self::build_department_child($v['id'], $area_list);
                if (!empty($child)) {
                    $node['children'] = $child;
                }
                // todo 后续此处加上用户的权限判断
                $treeList[] = $node;
            }
        }
        return $treeList;
    }
}